home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Web Server / PHP.EXE / pear / PHPDoc / xmlexporter / PhpdocXMLExporter.php < prev    next >
Encoding:
PHP Script  |  2001-02-18  |  4.0 KB  |  145 lines

  1. <?php
  2. /**
  3. * Exporter used to export phpdoc internals data structures as xml documents.
  4. *
  5. * @version  $Id: PhpdocXMLExporter.php,v 1.3 2001/02/18 16:53:00 uw Exp $
  6. */
  7. class PhpdocXMLExporter extends PhpdocObject {
  8.     
  9.     /**
  10.     * Filename prefix for the generated xml document.
  11.     * 
  12.     * This class variable must be overriden by all derived classes.
  13.     * PHPDoc uses the filename prefix to detect the content of 
  14.     * the file.
  15.     * 
  16.     * @var  string  $fileprefix
  17.     */
  18.     var $fileprefix = "";
  19.  
  20.     /**
  21.     * Target directory where the xml documents get saved.
  22.     *
  23.     * @var  string      $path
  24.     * @see  setPath()
  25.     */    
  26.     var $path = "";
  27.     
  28.     /**
  29.     * Data to save as a xml document.
  30.     *
  31.     * @var  array       $result
  32.     * @see  setResult(), export()
  33.     */
  34.     var $result = array();
  35.     
  36.     /**
  37.     * Instance of PhpdocXMLWriter used to generate the xml document.
  38.     *
  39.     * @var  object  PhpdocXMLWriter
  40.     * @see  PhpdocXMLExporter()
  41.     */
  42.     var $xmlwriter;
  43.     
  44.     /**
  45.     * Creates a PhpdocXMLWriter object.
  46.     *
  47.     * Make sure that all derived classes call this constructor.
  48.     * 
  49.     * @see  $xmlwriter
  50.     */                                                            
  51.     function PhpdocXMLExporter() {
  52.     
  53.         $this->xmlwriter = new PhpdocXMLWriter;
  54.         
  55.     } // end constructor                                            
  56.  
  57.     /**
  58.     * Sets the target path for the generated xml documents.
  59.     *  
  60.     * @param    string
  61.     * @see      $path
  62.     * @access   public
  63.     */    
  64.     function setPath($path) {
  65.         $this->path = $path;
  66.     } // end func setPath
  67.     
  68.     /**
  69.     * Exports the given result array as xml document.
  70.     *
  71.     * @param    array    
  72.     * @param    string    name of the target xml file
  73.     * @access   public
  74.     * @see      create(), $result
  75.     */
  76.     function export($result, $xmlfile="") {
  77.         
  78.         if (0 == count($result))
  79.             return;
  80.  
  81.         $this->result = $result;
  82.         
  83.         $this->xmlwriter->addXML('<?xml version="1.0"?>');
  84.         $this->xmlwriter->startElement("phpdoc", "", "", false, true);
  85.  
  86.         $this->create();
  87.  
  88.         $this->xmlwriter->endElement("phpdoc", true);
  89.         
  90.         if ("" == $xmlfile)
  91.             $xmlfile = $this->result["name"];
  92.         
  93.         /*
  94.         if (file_exists($this->path.$xmlfile)) {
  95.             $i = 1;
  96.             while (file_exists($this->path.$name."_".$i.".xml"))
  97.                 $i++;
  98.                 
  99.             $xmlfile =    $name."_".$i.".xml";
  100.         }
  101.         */
  102.         
  103.         $xmlfile = $this->nameToURL($xmlfile);
  104.         $xmlfile = $this->path . $this->fileprefix . $xmlfile . ".xml";
  105.         
  106.         $this->xmlwriter->export($xmlfile);
  107.         $this->xmlwriter->free();
  108.         
  109.     } // end func export
  110.     
  111.     /**
  112.     * @param    array
  113.     */
  114.     function setResult($result) {
  115.         $this->result = $result;
  116.         $this->create();
  117.     } // end func setResult
  118.     
  119.     /**
  120.     * Kind of array_intersect for xml attributes.
  121.     * 
  122.     * This functions takes a data array and a list of allowed fields in the data
  123.     * array. All of the allowed fields that exists in the data array will be 
  124.     * copied to returned array which looks like:
  125.     * $attribs[name] = array ( type => allowed[name], value => data[name] ). 
  126.     * This structure is used by PhpdocXMLWriter->addElement().
  127.     *
  128.     * @param    array   data array
  129.     * @param    array   array of allowed fields and their attribute type 
  130.     * @return   array   $attribs
  131.     */
  132.     function getAttributes($data, $allowed) {
  133.         
  134.         $attribs = array();
  135.         
  136.         reset($allowed);
  137.         while (list($tag, $type) = each($allowed)) 
  138.             if (isset($data[$tag])) 
  139.                 $attribs[$tag] = array( "type" => $type, "value" => $data[$tag] );
  140.                 
  141.         return $attribs;
  142.     } // end func getAttributes
  143.  
  144. } // end PhpdocXMLExporter
  145. ?>